capture log close
clear all
set maxvar 120000
set more off
pause on
# delimit;

/* Convert outreg estimates to dta file, add means (calendar time) */

/* ******************** */
/* **** PREP MEANS **** */
/* ******************** */

local demo_list 	 "all female male singno othmen intel phys   twopar parhigh parlow white black nonw remlow remhigh nonment nonphys firstlt6 first7to13 firstgt13" ;
local demo_abbr_list "a   f      m    s      o      i     p      t      h       l	  w     b     z	   x	  r   	  n   		y		f1	   f2		 f3 " ;
local demo_list2 	 	"ue97high 	ue97low uegrhigh 	uegrlow ueallhigh 	uealllow crimehigh 	crimelow 	demhigh demlow 	pophigh	poplow " ;
local demo_abbr_list2 	"u7h 		u7l 	ugh 		ugl 	uah 		ual 	 ch 		cl 			dh 		dl 		ph 		pl 		" ;

local a_label "all" ;
local o_label "othmen" ;
local n_label "nonment" ;
local i_label "intel" ;
local p_label "phys" ;
local y_label "nonphys" ;
local t_label "twopar" ;
local s_label "singno" ;
local m_label "male" ;
local f_label "female" ;
local w_label "white" ;
local b_label "black" ;
local z_label "nonw" ;
local h_label "parhigh" ;
local l_label "parlow" ;
local f1_label "firstlt6" ;
local f2_label "first7to13" ;
local f3_label "firstgt13" ;
local u7h_label "ue97high" ;
local u7l_label "ue97low" ;
local ugh_label "uegrhigh" ;
local ugl_label "uegrlow" ;
local uah_label "ueallhigh" ;
local ual_label "uealllow" ;


use prwora_crime_summstats, clear ;

keep if label == "all_mean"  	| label == "all_sd" 	| label == "all_median" | 
		label == "prec_mean" 	| label == "postt_mean"	| label == "prec_rd_mean" ;

xpose, clear varname ;

rename v1 mean_all ;
rename v2 sd_all ;
rename v3 median_all ;
rename v4 mean_trt ;
rename v5 mean_cntrl ;
rename v6 mean_rd ;

drop if _varname == "label" ;

rename _varname variables ;

order variables mean_all sd_all median_all mean_trt mean_cntrl mean_rd ;

gen demo = substr(variables, 1, 1) ;
replace variables = substr(variables,2,.) ;

foreach demo in `demo_list' { ;
	
	preserve ;
	
	keep if demo == "`demo'" ;

	drop demo ;

	tempfile means_prwora_``demo'_label' ;

	save `means_prwora_``demo'_label'' ;
	
	restore ;
} ;


use prwora_crime_summstats2, clear ;

keep if label == "all_mean"  	| label == "all_sd" 	| label == "all_median" | 
		label == "prec_mean" 	| label == "postt_mean"	| label == "prec_rd_mean" ;

xpose, clear varname ;

rename v1 mean_all ;
rename v2 sd_all ;
rename v3 median_all ;
rename v4 mean_trt ;
rename v5 mean_cntrl ;
rename v6 mean_rd ;

drop if _varname == "label" ;

order _varname mean_all sd_all median_all mean_trt mean_cntrl mean_rd ;

gen demo = substr(_varname, 1, 2) ;
replace demo = substr(_varname, 1, 3) if substr(_varname,1,1) == "u" ;

gen variables = substr(_varname,3,.) ;
replace variables = substr(_varname, 4, .) if substr(_varname,1,1) == "u" ;

foreach demo in `demo_abbr_list2' { ;
	
	preserve ;
	
	keep if demo == "`demo'" ;

	drop demo ;

	tempfile means_prwora_``demo'_label' ;

	save `means_prwora_``demo'_label'' ;
	
	restore ;
} ;





/* ************************************************ */
/* **** CONVERT IV ESTIMATES TO STATA DTA FILE **** */
/* ************************************************ */

foreach cov in no all{;

	foreach n of local num {;

		foreach exp in prwora {;
	
			if "`n'" == "1" | "`n'" == "2" {;
	
				foreach demo in `demo_list' `demo_list2' {;
				
					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};

					rename VARIABLES variables ;

					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};

					gen sample = "age18unfav" if n_age18unfav != . ;
					capture replace sample = "onssi1922" if n_onssi1922 != . ;
					
					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;


					save `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo', replace ;
					

				};
			
			};

			else if "`n'" == "3" | "`n'" == "4" {;
	
				foreach demo in all {;
				
					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};

					rename VARIABLES variables ;

					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};

					gen sample = "age18unfav" if n_age18unfav != . ;
					capture replace sample = "onssi1922" if n_onssi1922 != . ;

					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;

					
					save `exp'_bw`bw'_crime_poly`n'`cov'cov_iv_`demo', replace ;
				};
			
			}; /* end if */
			
		}; /* end exp */
	
	}; /* end n*/

}; /* end cov*/



/* ************************************************ */
/* **** CONVERT RD ESTIMATES TO STATA DTA FILE **** */
/* ************************************************ */

foreach cov in no all{;

	foreach exp in prwora {;
		
		foreach n of local num {;
			
			if "`n'" == "1" | "`n'" == "2" {;
			
				foreach demo in `demo_list' `demo_list2' {;

					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};

					rename VARIABLES variables ;

					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};
					
					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;

					save `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo', replace ;
				
				};
			
			};
			
			if "`n'" == "3" | "`n'" == "4" {;
			
				foreach demo in all {;

					import delimited `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo'.txt, varn(1) clear;

					egen nmcount = rownonmiss(_all), strok;

					drop if nmcount == 0 ;
					gen se = 1 if v1 == "" ;

					replace v1 = "se" + v1[_n-1] if se == 1 ;

					drop nmcount se ;

					sxpose, firstnames clear ;

					foreach var of varlist _all {;

						replace `var'   = subinstr(`var',   "*", "",.) ;
						replace `var'   = subinstr(`var',   "(", "",.) ;
						replace `var'   = subinstr(`var',   ")", "",.) ;
						replace `var'   = subinstr(`var',   ",", "",.) ;
					};

					rename VARIABLES variables ;

					#delim;
					quietly describe, varlist ;
					local vars `r(varlist)'  ;
					local string variables  ;
					local numeric : list vars - string ;
					display "`numeric'" ;

					foreach var of local numeric{;

						destring, replace;

					};
					
					merge m:1 variables using `means_`exp'_`demo'' ;
						
					drop if _merge == 2 ;
						
					drop _merge;

					save `exp'_bw`bw'_crime_poly`n'`cov'cov_rd_`demo', replace ;
				
				};
			};
		};
	};
};


/* **************************************************************** */
/* **** CONVERT JOINT CRIMExEMP IV ESTIMATES TO STATA DTA FILE **** */
/* **************************************************************** */

foreach cov in all {;
			
	foreach demo in 
			`demo_list'	crimehigh crimelow ueallhigh uealllow demhigh demlow pophigh poplow
			{;

		import delimited prwora_cXe_bw`bw'_crime_poly1`cov'cov_iv_`demo'.txt, varn(1) clear;

		egen nmcount = rownonmiss(_all), strok;

		drop if nmcount == 0 ;
		gen se = 1 if v1 == "" ;

		replace v1 = "se" + v1[_n-1] if se == 1 ;

		drop nmcount se ;

		sxpose, firstnames clear ;

		foreach var of varlist _all {;

			replace `var'   = subinstr(`var',   "*", "",.) ;
			replace `var'   = subinstr(`var',   "(", "",.) ;
			replace `var'   = subinstr(`var',   ")", "",.) ;
			replace `var'   = subinstr(`var',   ",", "",.) ;
		};

		rename VARIABLES variables ;

		quietly describe, varlist ;
		local vars `r(varlist)'  ;
		local string variables  ;
		local numeric : list vars - string ;
		display "`numeric'" ;

		foreach var of local numeric{;

			destring, replace;

		};

		save prwora_cXe_bw`bw'_crime_poly1`cov'cov_iv_`demo', replace ;
	
	};
};

